package gold.digger;

import gold.utils.InputUtil;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LC945 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int minIncrementForUnique(int[] nums) {
            int curZeroPos = 0, sum = 0, maxNum = -1;
            int[] cnt = new int[200001];
            for (int num : nums) {
                cnt[num]++;
                maxNum = Math.max(maxNum, num);
            }

            for (int i = 0; i <= maxNum; i++) {
                if (cnt[i] <= 1) continue;
                for (int j = 0; j < cnt[i] - 1; j++) {
                    if (curZeroPos <= i) curZeroPos = i + 1;
                    while (cnt[curZeroPos] != 0) ++curZeroPos;
                    sum += curZeroPos - i;
                    cnt[curZeroPos++] = 1;
                }
            }

            return sum;
        }
    }

    public void run() {
        Solution solution = new Solution();
//        int[] arr = InputUtil.toIntegerArray("[3,2,1,2,1,7]");
//        System.out.println(solution.minIncrementForUnique(arr));

        System.out.println(solution.minIncrementForUnique(InputUtil.toIntegerArray("[1,2,2]")));
    }

    public static void main(String[] args) throws Exception {
        LC945 an = new LC945();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
